import java.util.concurrent.CountDownLatch;

/**
 * Created by admin on 2019/1/17.
 */

public class CountDownTest{

    public static void main(String[] args) throws InterruptedException {
        int works = 20;
        CountDownLatch cd = new CountDownLatch(works);
        for (int i = 1; i <= works; i++) {
            new Thread(new Worker(cd, i+"")).start();
        }
        cd.await();
        System.out.println("Worker工作完成了");
    }
}
class Worker implements Runnable {
    CountDownLatch countDownLatch;
    String content;

    public Worker(CountDownLatch countDownLatch, String content) {
        this.countDownLatch = countDownLatch;
        this.content = content;
    }
    public void run() {
        try {
            Thread.sleep(1000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
        System.out.println("工作线程" + Thread.currentThread().getId() + "完成了工作[" + content + "]");
        countDownLatch.countDown();
//        try {
//            countDownLatch.await();
//            System.out.println("工作线程" + Thread.currentThread().getId() +" await=============");
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
    }
}
